﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class ChonMon : Form
    {
        public ChonMon()
        {
            InitializeComponent();
        }

        public List<MonAnDTO> listMonAn = new List<MonAnDTO>();
        private DataTable dataTable = new DataTable();
        public bool huy = true;


        private void Search()
        {
            int vt = -1;
            for (int i = dataGridView_ChonMonAn.RowCount - 1; i >= 0; i--)
            {
                dataGridView_ChonMonAn.Rows[i].Visible = false;
                if (dataGridView_ChonMonAn.Rows[i].Cells[clTenMonAn.Index].Value.ToString().ToUpper().IndexOf(txt_TimKiem.Text.ToString().ToUpper()) >= 0)
                {
                    vt = i;
                    dataGridView_ChonMonAn.Rows[i].Visible = true;
                }
            }

            if (vt == -1)
            {
                btn_Xong.Enabled = false;
            }
            else
            {
                dataGridView_ChonMonAn.CurrentCell = dataGridView_ChonMonAn.Rows[vt].Cells[clCheck.Index];
                dataGridView_ChonMonAn.CurrentCell.Selected = true;
                btn_Xong.Enabled = true;
            }
        }

        private void ChonMon_Load(object sender, EventArgs e)
        {
            int temp = 0;
            
            dataTable = MonAnBUS.SelectMonAnAll(ref temp);

            int i = 1;
            foreach (DataRow item in dataTable.Rows)
            {
                dataGridView_ChonMonAn.Rows.Add(i, item["Ma_Mon"].ToString() ,item["TenMon"].ToString(), item["Loai"].ToString(), item["GiaTien"].ToString(),false);
                i++;
            }
        }

        private void btn_Xong_Click(object sender, EventArgs e)
        {
            bool flag = false;
            
            
            for (int i = 0; i < dataGridView_ChonMonAn.RowCount; i++)
            {
                if (dataGridView_ChonMonAn.Rows[i].Cells[clCheck.Index].Value.ToString() == "True")
                {
                    MonAnDTO monAnDTO = new MonAnDTO();
                    monAnDTO.Ma_Mon = dataGridView_ChonMonAn.Rows[i].Cells["clMaMonAn"].Value.ToString();
                    monAnDTO.TenMon = dataGridView_ChonMonAn.Rows[i].Cells["clTenMonAn"].Value.ToString();
                    monAnDTO.Loai = dataGridView_ChonMonAn.Rows[i].Cells["clLoai"].Value.ToString();
                    monAnDTO.GiaTien = float.Parse(dataGridView_ChonMonAn.Rows[i].Cells["clGiaTien"].Value.ToString());

                    //monAnDTO.Ma_Mon = dataTable.Rows[i]["Ma_Mon"].ToString();
                    //monAnDTO.TenMon = dataTable.Rows[i]["TenMon"].ToString();
                    //monAnDTO.Loai = dataTable.Rows[i]["Loai"].ToString();
                    //monAnDTO.GiaTien = float.Parse(dataTable.Rows[i]["GiaTien"].ToString());

                    listMonAn.Add(monAnDTO);                    
                    flag = true;
                }
            }

            if (flag == false)
            {
                DialogResult result = MessageBox.Show("Chưa có món ăn nào được chọn, bạn muốn thoát?", "Món ăn", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                {
                    huy = true;
                    this.Dispose();
                }
            }
            else
            {
                huy = false;
                this.Dispose();
            }
 
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            huy = true;
            this.Dispose();
            Dispose();
        }

        private void txt_TimKiem_TextChanged(object sender, EventArgs e)
        {
            Search();
        }            
    }
}
